package com.lightsaber.trade.cloth.web.category;

import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.utils.web.struts2.Struts2Utils;

import com.lightsaber.trade.core.web.BaseAction;
import com.lightsaber.trade.cloth.entity.category.Category;
import com.lightsaber.trade.cloth.entity.category.CategoryLeaf;
import com.lightsaber.trade.cloth.entity.category.CategoryNode;
import com.lightsaber.trade.cloth.entity.category.CategoryNodeType;
import com.lightsaber.trade.cloth.entity.category.InputType;
import com.lightsaber.trade.cloth.entity.category.SourceType;
import com.lightsaber.trade.cloth.service.category.CategoryNodeService;
import com.lightsaber.trade.cloth.service.category.CategoryPlannerService;
import com.lightsaber.trade.cloth.service.category.CategoryService;
import com.lightsaber.trade.cloth.service.param.ParamService;

@Namespace("/category")
@Action(value = "categoryLeaf")
@Results({ @Result(name = "create-edit-category-node", location = "create-edit-category-node.jsp") })
public class CategoryLeafAction extends BaseAction {

    private static final long serialVersionUID = -6133932528005700711L;

    private static final Logger log = LoggerFactory.getLogger(CategoryLeafAction.class);
    @Autowired
    private transient CategoryNodeService categoryNodeService;
    @Autowired
    private transient CategoryService categoryService;
    @Autowired
    private transient ParamService paramService;
    @Autowired
    private transient CategoryPlannerService plannerService;

    private CategoryLeaf categoryNode;
    private String parentId;
    private String categoryId;
    private String categoryNodeId;
    private String categoryNodeType;
    private String treeType;

    private Map<String, String> inputTypeMap;
    private Map<String, String> sourceTypeMap;

    @Override
    public String execute() {
        return list();
    }

    public String list() {
        try {
            categoryNode = categoryNodeService.findLeafById(categoryNodeId);
            setCategoryNodeId(categoryNodeId);
            if (categoryNode.getParent() != null) {
                setParentId(categoryNode.getParent().getId());
            }
            setCategoryId(categoryId);
            Category category = categoryService.get(categoryId);
            if (StringUtils.isBlank(category.getBranchName())) {
                setTreeType("Root");
            } else {
                setTreeType("Branch");
            }
            setCategoryNodeType(CategoryNodeType.Leaf.name());
            setInputTypeMap(InputType.list());
            setSourceTypeMap(SourceType.list());
        } catch (Exception e) {
            log.error(" list error :{}", e);
            addActionError(e.getMessage());
        }
        return "create-edit-category-node";
    }

    public String createLeaf() {
        setParentId(categoryNodeId);
        setCategoryId(categoryId);
        setCategoryNodeId("");
        categoryNode = null;
        setCategoryNodeType(CategoryNodeType.Leaf.name());
        setTreeType(treeType);
        setInputTypeMap(InputType.list());
        setSourceTypeMap(SourceType.list());
        return "create-edit-category-node";
    }

    public String save() {
        try {
            CategoryNode parent = categoryNodeService.findById(parentId);
            categoryNode.setParent(parent);
            categoryNodeService.saveLeafFromPage(categoryNode);

            if (StringUtils.isBlank(categoryNodeId)) {
                plannerService.inheritPlanners(categoryNode);
            }
            // setCategoryNode(categoryNode);
            categoryNodeId = categoryNode.getId();

            addActionMessage(getText(PROCESSED_SUCCESS));

            return list();
        } catch (Exception e) {
            log.info(" save error :{}", e);
            addActionError(e.getMessage());
            if (StringUtils.isBlank(categoryNodeId)) {
                return createLeaf();
            } else {
                return list();
            }

        }

    }

    @Override
    public String input() throws Exception {
        return INPUT;
    }

    public void deleteLeaf() {

        try {
            categoryNodeService.deleteById(categoryNodeId);
            Struts2Utils.renderText("success");
        } catch (Exception e) {
            addActionError(e.getMessage());
            log.info("delete leaf error : {}", e.getMessage());
            Struts2Utils.renderText(e.getMessage());
        }
    }

    public CategoryLeaf getCategoryNode() {
        return categoryNode;
    }

    public void setCategoryNode(final CategoryLeaf categoryNode) {
        this.categoryNode = categoryNode;
    }

    public String getParentId() {
        return parentId;
    }

    public void setParentId(final String parentId) {
        this.parentId = parentId;
    }

    public String getCategoryNodeId() {
        return categoryNodeId;
    }

    public void setCategoryNodeId(final String categoryNodeId) {
        this.categoryNodeId = categoryNodeId;
    }

    public String getCategoryNodeType() {
        return categoryNodeType;
    }

    public void setCategoryNodeType(final String categoryNodeType) {
        this.categoryNodeType = categoryNodeType;
    }

    public String getCategoryId() {
        return categoryId;
    }

    public void setCategoryId(final String categoryId) {
        this.categoryId = categoryId;
    }

    public String getTreeType() {
        return treeType;
    }

    public void setTreeType(final String treeType) {
        this.treeType = treeType;
    }

    public Map<String, String> getInputTypeMap() {
        return inputTypeMap;
    }

    public void setInputTypeMap(final Map<String, String> inputTypeMap) {
        this.inputTypeMap = inputTypeMap;
    }

    public Map<String, String> getSourceTypeMap() {
        return sourceTypeMap;
    }

    public void setSourceTypeMap(final Map<String, String> sourceTypeMap) {
        this.sourceTypeMap = sourceTypeMap;
    }

    public ParamService getParamService() {
        return paramService;
    }

    public void setParamService(final ParamService paramService) {
        this.paramService = paramService;
    }

}
